<?php
/*******************************************************************************
*
* @Author Kevin Kunkel <kevin.kunkel@marist.edu>
*
* CMBGenericWebService contains an instance of SoapClient that connects to the 
* eclient and is used to search and retrieve content. There are three functions:
* search( $query ), getItem( $URI ), and getLastRequest(). 
*
* search performs a search with the given query. This must be a properly formed
* Content Manager search query. See IBM documentation for examples. It returns
* an XML document containing the pertinent metadata, including the URI.
*
* getItem retrieves a specific item given the URI that search returns. As
* currently implemented, it will render the content. 
*
*******************************************************************************/

class CMBGenericWebService{
   protected $client;

   public function CMBGenericWebService($database, $user, $password){
      $this->client = new SoapClient( "http://cmeclient.is.marist.edu:9082/CMBGenericWebService/services/CMBGenericWebService?wsdl");
		$this->database = $database;
		$this->user = $user;
		$this->password = $password;
   }
   public function search($query, $results){
      $string = '<RunQueryRequest maxResults="'.$results.'" contentOption="URL" retrieveOption="ITEMTREE" xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:cbm="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:cm="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
         <AuthenticationData connectString="SCHEMA=ICMCRTOR">
            <ServerDef>
               <ServerType>ICM</ServerType>
               <ServerName>'.$this->database.'</ServerName>
            </ServerDef>
            <LoginData>
               <UserID>'.$this->user.'</UserID>
               <Password>'.$this->password.'</Password>
            </LoginData>
         </AuthenticationData>
         <QueryCriteria>
            <QueryString>'.$query.'</QueryString>
         </QueryCriteria>
      </RunQueryRequest>';

      $params->arg_0_0 = $string;
      $params->arg_1_0 = null;
      $params->arg_2_0 = null;
      $params->arg_3_0 = null;
      $result = $this->client->processXMLRequest($params);
		return $result;
   }
   public function getItem($URI){
      $string = '<RetrieveItemRequest contentOption="URL" retrieveOption="CONTENT" xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema">
         <AuthenticationData connectString="SCHEMA=ICMCRTOR">
            <ServerDef>
               <ServerType>ICM</ServerType>
                <ServerName>'.$this->database.'</ServerName>
            </ServerDef>
            <LoginData>
               <UserID>'.$this->user.'</UserID>
               <Password>'.$this->password.'</Password>
            </LoginData>
         </AuthenticationData>
			<Item URI="'.$URI.'"/>
		</RetrieveItemRequest>';
      $params->arg_0_0 = $string;
      $params->arg_1_0 = null;
      $params->arg_2_0 = null;
      $params->arg_3_0 = null;
      $result = $this->client->processXMLRequest($params);
      $xml_parser = xml_parser_create();
      xml_parse_into_struct($xml_parser, $result->processXMLRequestReturn->xmlResponseText,$vals,$index);
      xml_parser_free($xml_parser);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $vals[$index["CM:URL"][0]]["attributes"]["VALUE"]);
      curl_setopt($ch, CURLOPT_HEADER, false);
      header("Content-type: " . $vals[$index["CM:RESOURCEOBJECT"][0]]["attributes"]["MIMETYPE"]);
      $content = curl_exec($ch);
      curl_close($ch);
   }
   public function getLastRequest(){
      return $this->client->__getLastRequest();
   }
}
// vim: ts=3:sw=3
?>
